package com.auto.learning.activity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.auto.learning.R;
import com.auto.learning.adapter.ImageGridAdapter;
import com.auto.learning.bean.ImageItem;
import com.auto.learning.net.HttpRequestHelper;
import com.auto.learning.tools.ImageFetcher;
import com.auto.learning.tools.IntentConstants;

/**
 * 图片选择
 */
public class ImageChooseActivity extends Activity{
	private List<ImageItem> mDataList = new ArrayList<ImageItem>();
	private String mBucketName;
	private int availableSize;
	private GridView mGridView;
	private TextView mBucketNameTv,tv_action_right;
	private TextView cancelTv;
	private ImageGridAdapter mAdapter;
	private Button mFinishBtn;
	private HashMap<String, ImageItem> selectedImgs = new HashMap<String, ImageItem>();
	private boolean isFirst = true;

	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_image_choose);
		initView();
		isFirst = false;
		initListener();
	}
	
	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
		initView();
	}

	private void initView(){
		if (isFirst) {
			ImageFetcher.getInstance(this).getImagesBucketList(true);
			mDataList = ImageFetcher.getInstance(this).getAllImages();
		}else 
			mDataList = (List<ImageItem>) getIntent().getSerializableExtra(IntentConstants.EXTRA_IMAGE_LIST);
		if (mDataList == null) mDataList = new ArrayList<ImageItem>();
		mBucketName = getIntent().getStringExtra(IntentConstants.EXTRA_BUCKET_NAME);
		if (TextUtils.isEmpty(mBucketName)){
			mBucketName = "请选择";
		}
		availableSize = getIntent().getIntExtra(IntentConstants.EXTRA_CAN_ADD_IMAGE_SIZE, HttpRequestHelper.MAX_IMAGE_SIZE);
		
		tv_action_right = (TextView) findViewById(R.id.tv_action_right);
		tv_action_right.setText("相册");
		tv_action_right.setTextColor(getResources().getColor(R.color.main_green_color));
		tv_action_right.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(ImageChooseActivity.this, ImageBucketChooseActivity.class);
				intent.putExtra(IntentConstants.EXTRA_CAN_ADD_IMAGE_SIZE, availableSize);
				startActivity(intent);
			}
		});
		mBucketNameTv = (TextView) findViewById(R.id.title);
		mBucketNameTv.setText(mBucketName);
		mGridView = (GridView) findViewById(R.id.gridview);
		mGridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
		mAdapter = new ImageGridAdapter(ImageChooseActivity.this, mDataList);
		mGridView.setAdapter(mAdapter);
		mFinishBtn = (Button) findViewById(R.id.finish_btn);
		cancelTv = (TextView) findViewById(R.id.tv_action_left);
		mFinishBtn.setText("完成" + "(" + selectedImgs.size() + "/" + availableSize + ")");
		mAdapter.notifyDataSetChanged();
	}

	private void initListener()	{
		mFinishBtn.setOnClickListener(new OnClickListener(){

			public void onClick(View v){
				Intent intent = new Intent(ImageChooseActivity.this, PublicCommentActivity.class);
				intent.putExtra(IntentConstants.EXTRA_IMAGE_LIST,(Serializable) new ArrayList<ImageItem>(selectedImgs.values()));
				startActivity(intent);
				finish();
			}
		});

		mGridView.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id){
				ImageItem item = mDataList.get(position);
				if (item.isSelected){
					item.isSelected = false;
					selectedImgs.remove(item.imageId);
				}else{
					if (selectedImgs.size() >= availableSize){
						Toast.makeText(ImageChooseActivity.this, "最多选择" + availableSize + "张图片",Toast.LENGTH_SHORT).show();
						return;
					}
					item.isSelected = true;
					selectedImgs.put(item.imageId, item);
				}
				mFinishBtn.setText("完成" + "(" + selectedImgs.size() + "/" + availableSize + ")");
				mAdapter.notifyDataSetChanged();
			}
		});

		cancelTv.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v){
				finish();
			}
		});
	}
}